package com.czl.module_service.viewmodel

import androidx.databinding.ObservableField
import com.annimon.stream.Collectors
import com.annimon.stream.Stream
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.RoomBean
import com.czl.base.data.bean.tengyun.HousesPaymentBean
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriberHelper
import com.czl.base.util.RxThreadHelper
import java.util.*

class HousesPaymentListViewModel (application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {

    val uc = UiChangeEvent()
    var tabIndex = ObservableField(1)
    var totalAmount = ObservableField("0.00")

    class UiChangeEvent {
        val loadCompleteEvent: SingleLiveEvent<HousesPaymentBean> = SingleLiveEvent()
        val loadFinallyEvent: SingleLiveEvent<Unit?> = SingleLiveEvent()
    }

    val onLoadMoreListener: BindingCommand<Void> = BindingCommand(BindingAction {
        getRoomIdList()
    })

    val onRefreshListener: BindingCommand<Void> = BindingCommand(BindingAction {
        getRoomIdList()
    })

    fun getRoomIdList() {
        model.getUserRooms(model.getLoginPhone().toString(),model.getAreaId())
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .doFinally {
                uc.loadFinallyEvent.postValue(null)
            }
            .subscribe(
                object : ApiSubscriberHelper<BaseBean<List<RoomBean>>>() {
                    override fun onResult(t: BaseBean<List<RoomBean>>) {
                        if (t.code == 200) {
                            t.data?.let {
                                val rooms = Stream.of(it).map { e -> e.houseId }
                                    .collect(Collectors.toList())
                                if (rooms != null) {
                                    getBillByRoomIds(roomIdList = rooms)
                                }
                            }
                            dismissLoading()
                        } else {
                            showErrorToast(t.msg)
                            dismissLoading()
                        }
                    }
                    override fun onFailed(msg: String?) {
                        showErrorToast(msg)
                        dismissLoading()
                    }

                }
            )
    }

    private fun getBillByRoomIds(
        roomIdList : List<String>? = null
    ) {
        val tUserId = model.getUserId()
        val payStatus = if (tabIndex.get() == 1) 0 else 1
        model.getBillByRoomIdList(tUserId, payStatus, roomIdList!!)
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<HousesPaymentBean>>(){
                override fun onResult(t: BaseBean<HousesPaymentBean>) {
                    if (t.state == 0){
                        uc.loadCompleteEvent.postValue(t.data)
                    }else{
                        uc.loadCompleteEvent.postValue(null)
                    }
                    dismissLoading()
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                    uc.loadCompleteEvent.postValue(null)
                }

            })
    }
}